﻿using RobosimAPI.Class;
using System.Data.SQLite;
using System.Text.Json;

namespace RobosimAPI.Command
{
	public static partial class Command
	{
		public static void GetThemeRuleDetails()
		{
			ThemeRule[] themeRules= ThemeRule.GetAllThemeRules();
			foreach (ThemeRule themeRule in themeRules)
			{
				string httpResult = Tools.SignHttpPost("https://robosim.zmrobo.com/back/Api2/Client/themeRuleDetails", "user_id=6&region_id=0&rule_id=" + themeRule.id, Tools.GetTimestamp().ToString(), "fbebfb2cfd9f9357f3d281b2f5c53448", "6");
				RuleMap themeMap = JsonSerializer.Deserialize<RuleMap>(httpResult);
				themeRule.introduction=themeMap.introduction;
				themeRule.rule_file=themeMap.ruleFile;
				themeRule.cover=themeMap.cover;
				themeRule.map_cover=themeMap.map_cover;
				themeRule.UpdateAll();	
				using (var connection = new SQLiteConnection(Account.DB_CONNECTION_STRING))
				{
					connection.Open();
					using (var transaction = connection.BeginTransaction())
					{
						using (var command = new SQLiteCommand(connection))
						{
							for (int i = 0; i < themeMap.ruleMap.Count; i++)
							{
                                Console.WriteLine("找到:"+ themeMap.ruleMap[i].title);
                                command.CommandText = "INSERT INTO themeRuleDetails (id, title, difficulty, routine, robot, program, sort, [limit], md5, map_pwd, map_url,theme_rule) VALUES (@id, @title, @difficulty, @routine, @robot, @program, @sort, @limit, @md5, @map_pwd, @map_url, @theme_rule)";
								command.Parameters.AddWithValue("@id", themeMap.ruleMap[i].id);
								command.Parameters.AddWithValue("@title", themeMap.ruleMap[i].title);
								command.Parameters.AddWithValue("@difficulty", themeMap.ruleMap[i].difficulty);
								command.Parameters.AddWithValue("@routine", themeMap.ruleMap[i].routine);
								command.Parameters.AddWithValue("@robot", themeMap.ruleMap[i].robot);
								command.Parameters.AddWithValue("@program", themeMap.ruleMap[i].program);
								command.Parameters.AddWithValue("@sort", themeMap.ruleMap[i].sort);
								command.Parameters.AddWithValue("@limit", themeMap.ruleMap[i].limit);
								command.Parameters.AddWithValue("@md5", themeMap.ruleMap[i].md5);
								command.Parameters.AddWithValue("@map_pwd", themeMap.ruleMap[i].map_pwd);
								command.Parameters.AddWithValue("@map_url", themeMap.ruleMap[i].map_url);
								command.Parameters.AddWithValue("@theme_rule", themeRule.id);
								command.ExecuteNonQuery();
								byte[] mapBytes = Tools.HttpGetBytes("http://oss.zmrobo.com/"+themeMap.ruleMap[i].map_url);
								Tools.WriteToFile("H:\\桌面\\nginx\\"+themeMap.ruleMap[i].map_url, mapBytes);
								if (!string.IsNullOrEmpty(themeMap.ruleMap[i].program))
								{
									byte[] programBytes = Tools.HttpGetBytes("http://oss.zmrobo.com/" + themeMap.ruleMap[i].program);
									Tools.WriteToFile("H:\\桌面\\nginx\\" + themeMap.ruleMap[i].program, programBytes);
								}
								if (!string.IsNullOrEmpty(themeMap.ruleMap[i].robot))
								{
									byte[] robotBytes = Tools.HttpGetBytes("http://oss.zmrobo.com/" + themeMap.ruleMap[i].robot);
									Tools.WriteToFile("H:\\桌面\\nginx\\" + themeMap.ruleMap[i].robot, robotBytes);
								}
							}
						}
						transaction.Commit(); // 提交事务
					}
				}
			}
		}
		public class RuleMapItem
		{
			public int id { get; set; }
			public string title { get; set; }
			public int difficulty { get; set; }
			public int routine { get; set; }
			public string robot { get; set; }
			public string program { get; set; }
			public int sort { get; set; }
			public string limit { get; set; }
			public string md5 { get; set; }
			public string map_pwd { get; set; }
			public string map_url { get; set; }
		}
		public class RuleMap
		{
			public int status { get; set; }
			public string cover { get; set; }
			public string map_cover { get; set; }
			public string introduction { get; set; }
			public string ruleFile { get; set; }
			public List<RuleMapItem> ruleMap { get; set; }
		}

	}
}
